package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrCalcFeeUtil;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SimCardTypeAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Sim卡类型属性联动处理类 field_name:200003678
 */
@Service
@AttrHandler(SimCardTypeAttrHandler.class)
public class SimCardTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String cateId =  CuccPosServiceUtils.getCateId(data.getSceneInstId(),data.getCustId());
        if (KiteStringUtils.equals(cateId,"2004072")){
            //雁飞5G 智连专线产品连接 “sim卡类型”为eSIM电子卡时联动展示“每张实体卡分配电子卡数量”
            //每张实体卡分配电子卡数量
            KiteAttr kiteAttr = inst.getAttr("210008198");
            if (Objects.nonNull(kiteAttr)){
                if (KiteStringUtils.equals("100007001", attr.getValue())) {
                    kiteAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_D);
                } else {
                    kiteAttr.setShowType(LocalKeyConsts.PROD_ATTR_SHOW_TYPE_E);
                }
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), kiteAttr);
            }
            return;
        }
        //企业自采
        KiteAttr enterPriseSelfPurchaseAttr = inst.getAttr("200003676");
        if (Objects.nonNull(enterPriseSelfPurchaseAttr)) {
            //企业自采选择值为【是】,卡商要展示
            if ("1_0000106".equals(enterPriseSelfPurchaseAttr.getValue())) {
                KiteAttr kiteAttr = inst.getAttr("200004040");
                Optional.ofNullable(kiteAttr).ifPresent(kiteAttrs -> {
                    kiteAttrs.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    kiteAttrs.setIsNull(KeyConsts.IFTRUE_F);
                });
            }
        }
        //卡号数据服务费
        KiteAttr cardFeeAttr = inst.getAttr("200003679");
        if (Objects.nonNull(cardFeeAttr)) {
            String simCardFee;
            if (KiteStringUtils.equalsAny(attr.getValue(), "100007000", "100007001")) {
                //eSim卡类型
                double eSimCardFeeDoubleValue = LocalAttrCalcFeeUtil.calcESimCardFee(inst);
                simCardFee = String.valueOf(eSimCardFeeDoubleValue);
            }
            else {
                //普通卡类型
                simCardFee = LocalAttrCalcFeeUtil.calcNormalSimCardFee(inst);
            }

            if (KiteStringUtils.isEmpty(simCardFee) || "0.0".equals(simCardFee)) {
                cardFeeAttr.setValue("0");
                cardFeeAttr.setValueDesc("0");
            }
            else {
                cardFeeAttr.setValue(simCardFee);
                cardFeeAttr.setValueDesc(simCardFee);
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        String cateId =  CuccPosServiceUtils.getCateId(data.getSceneInstId(),data.getCustId());
        if (KiteStringUtils.equals(cateId,"2004072")){
            //雁飞5G 智连专线产品连接 不用此逻辑
            return;
        }
        // 4G定制连接产品-Cat.1
        if (KiteStringUtils.equalsAny(inst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.GIFT_DZCAT1_PROD, ProdCateUtil.GIFT_TYCAT1_PROD, ProdCateUtil.GIFT_DZ5G_PROD))) {
            //每张实体卡分配电子卡数量
            KiteAttr eachCartNumber = inst.getAttr("200004043");
            List<AttrValue> attrs = AttrValueCache.get(eachCartNumber.getAttrId());

            // 企业自采
            KiteAttr enterPriseSelfPurchaseAttr = inst.getAttr("200003676");

            if ("100007000".equals(attr.getValue())) { // 100007000 : eSIM卡
                // SIM卡类型为eSIM卡时展示 : 实体卡规格 200004041、预置电子卡 200004042、每张实体卡分配电子卡数量 200004043，卡商 200004040
                LocalAttrHelper.showAndNotNull(inst, "200004041", "200004042", "200004043", "200004040");
                // SIM卡类型为ESIM卡，每张实体卡分配电子卡数量枚举值为：0、1、2
                eachCartNumber.setAttrValues(KiteBeanUtils.copyPropsForList(attrs, KiteAttrValue.class));
            }
            else if ("100007001".equals(attr.getValue())) { // 100007001 : ESIM电子卡
                // SIM卡类型为eSIM电子卡时展示 : 每张实体卡分配电子卡数量 200004043
                LocalAttrHelper.showAndNotNull(inst, "200004043");
                LocalAttrHelper.hideAndNullable(inst, "200004041", "200004042");
                // SIM卡类型为ESIM电子卡，每张实体卡分配电子卡数量枚举值为：1、2
                List<AttrValue> filterAttrs = attrs.stream()
                    .filter(attr1 -> KiteStringUtils.equalsAny(attr1.getAttrValue(), "100002271", "100002754"))
                    .collect(Collectors.toList());
                eachCartNumber.setAttrValues(KiteBeanUtils.copyPropsForList(filterAttrs, KiteAttrValue.class));
                if (KiteStringUtils.equals("1_0000399", eachCartNumber.getValue())) {
                    eachCartNumber.setValue("100002271");
                }
                // 当sim卡类型不为ESIM卡时，判断企业自采的值，如果不为是，则隐藏卡商
                if (!"1_0000106".equals(enterPriseSelfPurchaseAttr.getValue())) {
                    LocalAttrHelper.hideAndNullable(inst, "200004040");
                }
         }
            else {
                LocalAttrHelper.hideAndNullable(inst, "200004041", "200004042", "200004043");
                // 当sim卡类型不为ESIM卡时，判断企业自采的值，如果不为是，则隐藏卡商
                if (!"1_0000106".equals(enterPriseSelfPurchaseAttr.getValue())) {
                    LocalAttrHelper.hideAndNullable(inst, "200004040");
                }
            }

        }
    }
}
